package com.example.shoppingcar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shoppingcar.R;
import com.example.shoppingcar.entity.Student;

import java.util.List;

public class StudentAdapter extends BaseAdapter {

    //上下文环境
    private Context context;
    //item布局资源ID
    private int layoutId;
    //数据源
    private List<Student> students;


    //构造方法
    public StudentAdapter(){

    }
    public StudentAdapter(Context context, int layoutId, List<Student> students) {
        this.context = context;
        this.layoutId = layoutId;
        this.students = students;
    }


    //返回数据源中的元素的数量
    @Override
    public int getCount() {
        return students.size();
    }

    //返回指定位置的元素
    @Override
    public Object getItem(int position) {
        return students.get(position);
    }

    //返回位置
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //1.获取布局文件对象
        view= LayoutInflater.from(context).inflate(layoutId,null);
        //2.获取布局文件中的控件对象
        ImageView ivHeader = view.findViewById(R.id.iv_header);
        TextView tvName = view.findViewById(R.id.tv_name);
        TextView tvNo = view.findViewById(R.id.tv_no);

        //3.获取据源中当前i位置的元素
        Student stu = students.get(i);

        //4.把获取到的数据显示到对于的控件中
        ivHeader.setImageResource(stu.getsHeader());
        tvName.setText(stu.getsName());
        tvNo.setText(stu.getsNo());

        return view;
    }
}
